library SmokeGrenade initializer init requires LegacySystem


	private function SmokeGrenadeConditions takes nothing returns boolean
		local integer i = GetSpellAbilityId()
		return i=='A055' or i=='A05T' or i=='A07A' or i =='A08Q'
	endfunction

	private function SmokeGrenade takes nothing returns nothing
		local unit marine = GetTriggerUnit()
		local unit u
		local unit missile 
		local location l = GetSpellTargetLoc()
		local real x = GetLocationX(l)
		local real y = GetLocationY(l)
		local player p = GetOwningPlayer(marine)
		local integer current = udg_Second
		if GetPlayerController(p)==MAP_CONTROL_COMPUTER then
			call FieldChat(marine,5.0, udg_Pcolors[GetConvertedPlayerId(p)] + GetUnitName(marine) + "|r : Smoke!" )
		else
			call FieldChat(marine,5.0, udg_Pcolors[GetConvertedPlayerId(p)] + GetPlayerName(p) + "|r : Smoke!" )
		endif
		call TriggerSleepAction(0.)
		call AttachSoundToUnit(gg_snd_pinpull3, marine)
		call SetSoundVolume(gg_snd_pinpull3, 127)
		call StartSound(gg_snd_pinpull3)
		call PolledWait(0.1)
		set missile = CreateUnit(Player(11),'h00H',GetUnitX(marine),GetUnitY(marine),0.0)
		call IssuePointOrder(missile,"attackground",x,y)
		call PolledWait(2.5)
		set u = CreateUnit(Player(11),'e00G',x,y,0.0)
		call UnitApplyTimedLife(u,'Bhwd',40.0)
		if (current-udg_SmokeLimit!=0) then
			set bj_meleeNearestMineDist = GetUnitX(u)
			set bj_lastTransmissionDuration = GetUnitY(u)
			set udg_soundpath = "war3mapImported\\Smoke3.mp3"
			call PlaySoundPath3D.execute()
		else
			set udg_SmokeLimit = current
		endif
		call RemoveLocation(l)
		call RemoveUnit(missile)
		set missile = null
		set marine = null
		set u = null
		set l = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
		call TriggerAddCondition(t,Condition(function SmokeGrenadeConditions))
		call TriggerAddAction(t,function SmokeGrenade)
	endfunction

endlibrary